/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package main.java.cn.webrx;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

/**
 * <p>Project: jse2204 - NioDirDemo
 * <p>Powered by webrx On 2022-09-27 10:49:38
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class NioDirDemo {

    public static void main(String[] args) throws Exception {
        //Files.copy(Paths.get("c:\\abc\\g1.jpg"),Paths.get("c:\\abc\\ggggggggggg.jpg"));
        Files.copy(Path.of("c:\\abc\\g1.jpg"),Path.of("c:\\abc\\g333.jpg"));
    }

    public static void ff(String[] args)throws Exception {
        //NIO 文件操作
        try{
            Files.writeString(Path.of("mmm.txt"),"hello 中国\n", StandardOpenOption.APPEND);
        }catch(Exception e){
            e.printStackTrace();
        }

        String src = "D:\\0824\\javase\\jse2204\\jse2204\\ch09\\src\\cn\\webrx\\NioDirDemo.java";
        System.out.println(Files.readAllLines(Path.of(src)).size());
        List<String> ss = Files.readAllLines(Path.of(src));
        for(int i = 0;i<ss.size();i++){
            System.out.printf("%2d、%s%n",i+1,ss.get(i));
        }

    }


    public static void dir(String[] args) throws Exception {
        //建立目录
        //Files.createDirectory(Path.of("user-abc"));
        //Files.createDirectories(Path.of("xxx/yyy/user"));

        //删除目录
        //Files.deleteIfExists(Path.of("user-abc"));

        //Files.delete(Path.of("user-abc"));

        //DirectoryStream<Path> paths = Files.newDirectoryStream(Path.of("."));
        //for(Path p : paths){
        //    System.out.println(p.getFileName());
        //    System.out.println(Files.isDirectory(p));
        //}

        //deldir(Path.of("xxx"));

        //Files.copy(Paths.get("c:\\abc\\g1.jpg"),Paths.get("c:\\abc\\ggggggggggg.jpg"));

        Files.copy(Paths.get("mmm.txt"),Paths.get("mmm2.txt"));
        System.out.println(Files.copy(Paths.get("mi.txt"), Paths.get("mi22222.txt")));


    }

    public static void deldir(Path path) {
        if (Files.isDirectory(path)) {
            try (var dirs = Files.newDirectoryStream(path)) {
                for (Path p : dirs) {
                    if (Files.isDirectory(p)) {
                        deldir(p);
                    } else {
                        Files.delete(p);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
